﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;

namespace IHISCETeim.Web.MachineSys.FTradeModel
{
    public partial class FTradeReceivablesTuiJi2 : PageBase
    {
        protected override string DWZRef
        {
            get
            {
                return "FTradeReceivablesListEasyUI";
                
            }
        }

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.应收帐款_操作 };
            }
        }



        protected int FTradeID
        {
            get
            {

                return GetRequestValue<int>("FTradeID", 0);
            }
        }

        private FTradeReceivablesInfo _objFx = null;

        protected FTradeReceivablesInfo obj
        {
            get
            {
                if (_objFx == null)
                    _objFx = FTradeReceivablesController.GetByFTradeID(FTradeID);
                return _objFx;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();

            }
        }


        private void addmenu()
        {
            //2013-09-24 加入退机时，月促销折让金额不能退，即退机仍有未收款
            FTradeReceivablesInfo objold = new FTradeReceivablesInfo();
            objold.FTradeID = FTradeID;
            objold.YueCuXiaoJinE = GetRequestValue<decimal>("YueCuXiaoJinE_Fu", 0);
            objold.HeTongJinE = GetRequestValue<decimal>("HeTongJinE_Fu", 0);
            objold.FTradeSalesId = obj.FTradeSalesId;
            //2013-09-24 处理退机仍有未收款项的
            FTradeAgeDetailInfo objDetail = new FTradeAgeDetailInfo();
            objDetail.FTradeSalesId = obj.FTradeSalesId;
            objDetail.ShowIndex = 999;
            objDetail.TongJiName = "一般销售";
            objDetail.ShowTitle = "月促销金额";
            objDetail.JinE = Math.Abs(obj.YueCuXiaoJinE - objold.YueCuXiaoJinE);
            objDetail.DaoQiRi = GetRequestValue<DateTime>("DaoQi_Fu", Convert.ToDateTime( DateTime.Now.AddMonths(1).ToString("yyyy-MM-01")).AddDays(-1));//默认本月最后一天
            objDetail.SuspendedMark = 0;
            objDetail.Remark = "退机时,仍有当时月促销金额未收回";
            objDetail.RealName = User.RealName;
            objDetail.YearRate = 0;
            objDetail.TanQeChenBen = 0;
            // objDetail.
            //
            FTradeReceivablesInfo objNewFu = Request.SerializableObject<FTradeReceivablesInfo>();

            int FCatheringNum = FTradeReceivablesController.AddTuiJi(objNewFu, objold, objDetail);

            // AddLog(Permission.修改, obj, objTrade);
           
            if (FCatheringNum == 0)
            {
                SuccessJson("已退机成功", this.DWZRef);
            }
            if (FCatheringNum < 0)
            {
                ErrorJson("退机失败");
            }
            
            if (FCatheringNum > 0)
            {
                string msg2 = "已成功退机,请转到[收款及冲帐]界面,对调帐金额进行处理.";
                string tabTitle = "收款及冲帐";
                string taburl = "FTradeModel/FTradeCatheringListEasyUI.aspx";
                string tabrelId = "FTradeCatheringListEasyUI";
                SuccessOpenNavTab(msg2, tabTitle, taburl, tabrelId, true);
            }
            //

        }
    }
}